package org.egomsl.mw;

import org.apache.hadoop.io.WritableComparator;

import java.util.Arrays;

public class BytesUtils {

    public static int compare(byte[] a, byte[] b) {
        return WritableComparator.compareBytes(a, 0, a.length, b, 0, b.length);
    }

    public static int compare(byte[] a, int aOffset, int aLen, byte[] b, int bOffset, int bLen) {
        return WritableComparator.compareBytes(a, aOffset, aLen, b, bOffset, bLen);
    }

    public static int FNVHash(byte[] data) {
        return FNVHash(data, 0, data.length);
    }

    public static int FNVHash(byte[] data, int offset, int len)
    {
        long hashCode = 0x811c9dc5;
        for (int i = offset; i < offset+len; ++i) {
            hashCode ^= data[i];
            hashCode *= 0x01000193;
        }

        return (int)hashCode;
    }
}
